import { createReadStream, promises as fs } from "node:fs"; import { Readable } from "node:stream"; import { join } from "node:path"; import { previewFor } from "@/lib/papers/queries"; import { STORAGE_DIR } from "@/lib/env"; export const dynamic = "force-dynamic"; /** First-page preview of the current manuscript: /api/preview/.png */ export async function GET(_req: Request, { params }: { params: Promise<{ name: string }> }) { const { name } = await params; const slug = name.replace(/\.png$/, ""); if (!/^[a-z0-9]+$/.test(slug)) return new Response("Not found", { status: 404 }); const sha = await previewFor(slug); if (!sha) return new Response("Not found", { status: 404 }); const path = join(STORAGE_DIR, "previews", `${sha}.png`); try { await fs.access(path); } catch { return new Response("Not found", { status: 404 }); } return new Response(Readable.toWeb(createReadStream(path)) as unknown as ReadableStream, { headers: { "Content-Type": "image/png", "Cache-Control": "public, max-age=604800, immutable", ETag: `"${sha}"` } }); }